﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_BLL;
using QLTV_Public;

namespace QLTV_GiaoDien
{
    public partial class frm_Lop_DonVi : DevExpress.XtraEditors.XtraForm
    {
        public frm_Lop_DonVi()
        {
            InitializeComponent();
        }
        public delegate void GetInt(int ma);
        public GetInt GetMa;
        public void GetLoad(int load)
        {
            txt_Load.Text = load.ToString();
        }
        DonVi_BLL donvi_bll = new DonVi_BLL();
        private void frm_Lop_DonVi_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = donvi_bll.LayDuLieu();
            txt_Load.Hide();
        }
        private void btn_Them_Click(object sender, EventArgs e)
        {
            frm_Lop_DonVi_Them f = new frm_Lop_DonVi_Them();
            f.Flag = 0;
            f.GetLoad = new frm_Lop_DonVi_Them.GetInt(GetLoad);
            f.ShowDialog();
        }

        private void btn_Sua_Click(object sender, EventArgs e)
        {
            frm_Lop_DonVi_Them f = new frm_Lop_DonVi_Them();
            f.Flag = 1;
            f.GetLoad = new frm_Lop_DonVi_Them.GetInt(GetLoad);
            f.MaDonVi = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Đơn Vị"));
            f.TenDonVi=gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Đơn Vị").ToString();
            f.GhiChu=gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Ghi Chú").ToString();
            f.ShowDialog();
        }
        NhatKy_BLL nhatKy_bll = new NhatKy_BLL();
        NhatKy_Public nk = new NhatKy_Public();
        TrungGian tg = new TrungGian();
        private void btn_Xoa_Click(object sender, EventArgs e)
        {
            DonVi_Public dv = new DonVi_Public();
            dv.MaDonVi = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Đơn Vị").ToString());
            dv.TenDonVi = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Đơn Vị").ToString();
            if (XtraMessageBox.Show("Bạn có muốn xóa Đơn vị: '" + dv.TenDonVi + "' có Mã là: '" + dv.MaDonVi + "' này không?", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {

                donvi_bll.Delete_DonVi(dv);
                //
                string d = DateTime.Now.ToString();
                nk.MaNV = tg.MaNV;
                nk.NgayGio = d;
                nk.HanhDong = "Xóa";
                nk.MoTa = "Xóa thông tin Đơn vị: '" + dv.TenDonVi + "'";
                nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                //
                frm_Lop_DonVi_Load(sender, e);
            }
        }

        private void btn_Chon_Click(object sender, EventArgs e)
        {
            GetMa(Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Đơn Vị")));
            this.Close();
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            GetMa(Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Đơn Vị")));
            this.Close();
        }
        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txt_Load_TextChanged(object sender, EventArgs e)
        {
            if (txt_Load.Text == "1")
            {
                frm_Lop_DonVi_Load(sender,e);
                txt_Load.Text = "0";
            }
        }
    }
}